package com.store_pro.service.impl;


import com.store_pro.entity.User;
import com.store_pro.enums.ResultEnum;
import com.store_pro.mapper.UserMapper;
import com.store_pro.service.IUserService;
import com.store_pro.storeException.ShellException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 用户业务层实现类
 * @author jasmine
 */
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public void reg(User user) {
        // 判断用户名是否已存在
        User result = userMapper.findByUsername(user.getUsername());
        // 如果已存在，抛出异常
        if (result != null) {
            // 抛出异常
            throw new ShellException(ResultEnum.USER_EXIST);
        }
        user.setCreatedUser(user.getUsername());
        user.setModifiedUser(user.getUsername());
        Date date = new Date();
        user.setCreatedTime(date);
        user.setModifiedTime(date);

        // 保存用户
        Integer count = userMapper.insert(user);
        // 判断结果
        if (count != 1){
            throw new ShellException(ResultEnum.USER_REGISTER_FAIL);
        }
    }
}
